﻿using System;

using Gonte.Reflection;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyUtilitiesTest
{
    [TestClass()]
    public class TypeAccessorTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public class AccessedClass
        {
            private String textData_;

            public String TextData
            {
                get { return textData_; }
                set { textData_ = value; }
            }

            private int integerData_;

            public int IntegerData
            {
                get { return integerData_; }
                set { integerData_ = value; }
            }
        }

        public interface IAccessed
        {
            String TextData
            {
                get;
                set;
            }

            int IntegerData
            {
                get;
                set;
            }
        }

        public struct AccessedStruct
            : IAccessed
        {
            private String textData_;

            public String TextData
            {
                get { return textData_; }
                set { textData_ = value; }
            }

            private int integerData_;

            public int IntegerData
            {
                get { return integerData_; }
                set { integerData_ = value; }
            }
        }

        [TestMethod()]
        public void TypeAccessorClassSetValueTest()
        {
            TypeAccessor accessor = TypeAccessor.GetTypeAccessor(typeof(AccessedClass));

            AccessedClass obj = new AccessedClass();
            accessor.SetValue(obj, "TextData", "Some text");
            accessor.SetValue(obj, "IntegerData", 427);

            Assert.AreEqual("Some text", obj.TextData);
            Assert.AreEqual(427, obj.IntegerData);
        }

        [TestMethod()]
        public void TypeAccessorClassGetValueTest()
        {
            TypeAccessor accessor = TypeAccessor.GetTypeAccessor(typeof(AccessedClass));

            AccessedClass obj = new AccessedClass();
            obj.TextData = "Some text";
            obj.IntegerData = 427;

            Assert.AreEqual("Some text", accessor.GetValue(obj, "TextData"));
            Assert.AreEqual(427, accessor.GetValue(obj, "IntegerData"));
        }

        [TestMethod()]
        public void TypeAccessorStructSetValueTest()
        {
            TypeAccessor accessor = TypeAccessor.GetTypeAccessor(typeof(AccessedStruct));

            AccessedStruct obj = new AccessedStruct();
            accessor.SetValue(obj, "TextData", "Some text");
            accessor.SetValue(obj, "IntegerData", 427);

            Assert.IsNull(obj.TextData);
            Assert.AreEqual(0, obj.IntegerData);
        }

        [TestMethod()]
        public void TypeAccessorStructSetValueUsingInterfaceTest()
        {
            TypeAccessor accessor = TypeAccessor.GetTypeAccessor(typeof(AccessedStruct));

            IAccessed obj = new AccessedStruct();
            accessor.SetValue(obj, "TextData", "Some text");
            accessor.SetValue(obj, "IntegerData", 427);

            Assert.AreEqual("Some text", obj.TextData);
            Assert.AreEqual(427, obj.IntegerData);
        }

        [TestMethod()]
        public void TypeAccessorStructGetValueTest()
        {
            TypeAccessor accessor = TypeAccessor.GetTypeAccessor(typeof(AccessedStruct));

            AccessedStruct obj = new AccessedStruct();
            obj.TextData = "Some text";
            obj.IntegerData = 427;

            Assert.AreEqual("Some text", accessor.GetValue(obj, "TextData"));
            Assert.AreEqual(427, accessor.GetValue(obj, "IntegerData"));
        }
    }
}
